package cn.xiyun.xiyuncc.web.controller;

import cn.xiyun.xiyuncc.domain.KillCourse;
import cn.xiyun.xiyuncc.dto.CourseKillDto;
import cn.xiyun.xiyuncc.service.IKillCourseService;
import cn.xiyun.xiyuncc.query.KillCourseQuery;
import cn.xiyun.xiyuncc.result.JSONResult;
import cn.xiyun.xiyuncc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;

    /**
     * 查询单个秒杀数据
     */
    @RequestMapping(value="kill",method= RequestMethod.POST)
    public JSONResult kill(@RequestBody CourseKillDto courseKillDto){

      String orderNo =  killCourseService.kill(courseKillDto);

        return JSONResult.success(orderNo);
    }
    /**
     * 查询单个秒杀数据
     */
    @RequestMapping(value="/online/one/{killId}/{activeId}",method= RequestMethod.GET)
    public JSONResult onlineOne(@PathVariable("killId") Long killId,@PathVariable("activeId")Long activeId){

       KillCourse killCourse =  killCourseService.onlineOne(killId,activeId);

        return JSONResult.success(killCourse);
    }

    /**
     * 查询秒杀数据
     */
    @RequestMapping(value="/online/all",method= RequestMethod.GET)
    public JSONResult onlineAll(){

           List<KillCourse> list =  killCourseService.onlineAll();

        return JSONResult.success(list);
    }

    /**
     * 添加秒杀课程
     */
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public JSONResult saveOrder(@RequestBody KillCourse killCourse){

            killCourseService.saveKillOrder(killCourse);

        return JSONResult.success();
    }



    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }
}
